<?php


namespace App\Service;

use Illuminate\Support\Facades\Session;

class Captcha
{

    protected $error;

    // 获取错误信息
    public function getError()
    {
        return $this->error;
    }


    // 生成验证码
    public function makeCode($name = '', $num = 4, $size = 20, $width = 0, $height = 0)
    {
        $name = $name ?: 'session_code';
        $code = strtolower(random($num));

		// 先删除在重新赋值 避免多次生成 形成数组
        Session::put($name, $code);

        return $this->makeImg($code, $size, $width, $height);
    }

    // 验证验证码
    public function checkCode($vdcode, $name = '')
    {
        if (empty($vdcode)) {
            $this->error = '请输入验证码！';
            return false;
        }

        $name = $name ?: 'session_code';
        if ($vdcode != Session::get($name)) {
            $this->error = '输入验证码错误！';
            return false;
        }

        return true;
    }

    // 随机字体文件
    protected function fonts()
    {
        ## 字体 captcha english arial FetteSteinschrift heiti number PilsenPlakat
        //$font     = 'http://static.buyuanshu.com/fonts/arial.ttf';
        //$font     = LIB_PATH.'core/fonts/arial.ttf';
        $path   = app()->basePath() . '/public/static/code';
        $fonts  = (new Dir())->readDir($path);
        $random = array_rand($fonts);
        $font   = $fonts[$random];

        return $font;
    }

    // 生成验证码图片
    protected function makeImg($code, $size = 20, $width = 0, $height = 0)
    {
        $font = $this->fonts();
        if (!is_file($font)) {
            $this->error = '字体文件不存在！';
            return false;
        }

        // 验证码图片参数
        $num = strlen($code);
        if (empty($width)) {
            $width = $num * $size * 4 / 5 + 5;
        }
        if (empty($height)) {
            $height = $size + 10;
        }


        // 画图像  定义要用到的颜色
        $im         = imagecreatetruecolor($width, $height);
        $back_color = imagecolorallocate($im, 235, 236, 237); // 定义背景颜色
        $boer_color = imagecolorallocate($im, 118, 151, 199); // 定义边框颜色
        $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));  // 定义随机字体颜色
        imagefilledrectangle($im, 0, 0, $width, $height, $back_color); // 画背景
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $boer_color); // 画边框
        imagefttext($im, $size, 0, 5, $size + 5, $text_color, $font, $code); // 画验证码

        // 画干扰线
        for($i = 0; $i < 10; $i++) {
            $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(-$width, $width), mt_rand(-$height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
        }
        // 画干扰点
        for($i = 0; $i < 50; $i++) {
            $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
        }


        //header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
        //header("Content-type: image/png;charset=utf8");
        ob_start();
        imagepng($im);
        $content = ob_get_contents();
        imagedestroy($im);

        return $content;
    }

}
